<?php

namespace app\model;

use think\facade\Db;
use think\Model;

class ProductShipping extends Model {

    protected $table ="product_shipping";

    protected $pk = 'product_shipping_id';

    private $cacheKey="product:shipping:id:";

    private $cacheTime=5184000;


    /*
   * 获取total
   * */
    public function getTotal($where=[]){
        return Db::name('product_shipping')->where($where)->count();
    }


    /*
     * 获取list
     * */
    public function getList($where=[],$page = null,$limit = null,$orderField = null,$orderType = "ASC"){

        $obj =  Db::name('product_shipping')->field("product_shipping_id")->where($where);


        if(!empty($page) && !empty($limit)){
            $obj = $obj->page($page, $limit);
        }

        if(!empty($orderField) && !empty($orderType)){
            $obj = $obj->order($orderField, $orderType);
        }

        $list = $obj->select();
        if($list){
            $return = [];
            foreach($list as $item){
                $return[] = $this->getInfo($item['product_shipping_id']);
            }
            return $return;
        }
        return [];
    }



    /*
* 获取订单状态详情
* */
    public function getInfo($id){
        $cacheKey = $this->cacheKey.$id;
        $redisModel = new RedisModel();
        $info = $redisModel->get($cacheKey);
        if($info){
            $info =  json_decode($info,true);
        }else{
            $info = Db::name('product_shipping')->find($id);
            $info["format_shipping_fee"] =  round( $info["shipping_fee"] / 100 ,2);
            $redisModel->set($cacheKey,json_encode($info,256),$this->cacheTime);
        }
        return $info;
    }



    /*
     * 获取商品售后服务属性列表
     * */
    public function getProductShipping($product_id){
        if(!$product_id) return [];
        $where =[];
        $where[] = ["product_id","=",$product_id];
        $where[] = ["product_shipping_status","=",1];
        $info = Db::name('product_shipping')
            ->field("product_shipping_id")
            ->where($where)
            ->find();
        return $this->getInfo($info["product_shipping_id"]);
    }


    /*
     * 获取商铺多个商品最大邮费
     * */
    public function getProductShippingFee($productIds){
        return Db::name('product_shipping')
            ->whereIn("product_id",$productIds)
            ->max("shipping_fee");
    }

    /*
     * 删除商品所有规格
     * */
    public function delProductShipping($product_id){
        $where =[];
        $where[] = ["product_id","=",$product_id];
        return $this->delAll($where);
    }


    /*
     * 删除操作
     * */
    public function delAll($where){
        return Db::name('product_shipping')->where($where)->delete();
    }


    /*
* 删除指定商品的缓存
**/

    public function delProductShippingCache($product_id){
        $where =[];
        $where[] = ["product_id","=",$product_id];
        $list = Db::name('product_shipping')->field("product_shipping_id")->where($where)->select();
        if($list){
            $redisModel = new RedisModel();
            foreach($list as $item){
                $redisModel->delete($this->cacheKey.$item["product_shipping_id"]);
            }
        }
    }


    /*
   * 删除全部商品的缓存
   **/

    public function delAllProductShippingCache(){
        $redisModel = new RedisModel();
        $redisModel->deleteAll($this->cacheKey);
    }
}